{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from contextlib import contextmanager\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "进入上下文前的操作\n",
      "执行上下文操作\n",
      "进入上下文后的操作\n"
     ]
    }
   ],
   "source": [
    "@contextmanager\n",
    "def task():\n",
    "    print(\"进入上下文前的操作\")\n",
    "    yield\n",
    "    print(\"进入上下文后的操作\")\n",
    "\n",
    "\n",
    "with task():\n",
    "    print(\"执行上下文操作\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "print(np.random.randint(0, 10))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "\n",
    "# 输入图片\n",
    "input = torch.randn((3, 256, 256))\n",
    "# RGB扰动\n",
    "rgbshift = np.random.normal(scale=0.02, size=(3, 1, 1))  # 均值为0，方差为0.02\n",
    "input += rgbshift\n",
    "# 加性噪声\n",
    "noise = np.random.normal(scale=0.02, size=(3, 256, 256))\n",
    "input += noise\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "[Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False),\n Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False),\n Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False),\n Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False),\n Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n ReLU(inplace=True),\n MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)]"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from torchvision import models\n",
    "\n",
    "torch.hub.set_dir('./cache')\n",
    "net = models.vgg16(pretrained=True)\n",
    "list(net.features)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "from torch import nn\n",
    "\n",
    "weight = nn.Parameter(torch.Tensor(64, 64))\n",
    "with torch.no_grad():\n",
    "    w = weight / weight.data.norm(keepdim=True, dim=0)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], [4, 5, 6, 7], [3, 4, 5]]\n",
      "[1, 2, 3, 4, 5, 6, 7, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "\n",
    "a = [1, 2, 3]\n",
    "b = [4, 5, 6, 7]\n",
    "c = [3, 4, 5]\n",
    "arr = [a, b, c]\n",
    "brr = reduce(lambda x, y: x + y, arr)\n",
    "print(arr)\n",
    "print(brr)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1, 2],\n       [1, 2]])"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "data = [1, 2]\n",
    "np.tile(data, (2, 1))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([3, 1])\n",
      "tensor([[1., 1., 1., 1.],\n",
      "        [2., 2., 2., 2.],\n",
      "        [3., 3., 3., 3.]])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "x = torch.Tensor([[1], [2], [3]])\n",
    "print(x.size())\n",
    "print(x.expand(3, 4))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[1, 2],\n",
      "         [3, 4]],\n",
      "\n",
      "        [[1, 2],\n",
      "         [3, 4]],\n",
      "\n",
      "        [[1, 2],\n",
      "         [3, 4]]])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "x = torch.tensor([[1, 2], [3, 4]])\n",
    "expanded = x.expand(3, 2, 2)\n",
    "\n",
    "print(expanded)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.Size([64, 50])"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features = torch.arange(64 * 64).reshape((64, 64))\n",
    "best_c = torch.arange(50 * 64).reshape((50, 64))\n",
    "\n",
    "t = features.expand(50, 64, 64).permute(1, 0, 2) - best_c.unsqueeze(0)\n",
    "\n",
    "torch.sum(torch.pow(t, 2), dim=2).shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0,  1,  2,  3,  4,  5],\n",
      "        [ 6,  7,  8,  9, 10, 11],\n",
      "        [12, 13, 14, 15, 16, 17],\n",
      "        [18, 19, 20, 21, 22, 23],\n",
      "        [24, 25, 26, 27, 28, 29],\n",
      "        [30, 31, 32, 33, 34, 35]])\n",
      "tensor([[[ 0,  1,  2,  3,  4,  5],\n",
      "         [ 6,  7,  8,  9, 10, 11],\n",
      "         [12, 13, 14, 15, 16, 17],\n",
      "         [18, 19, 20, 21, 22, 23],\n",
      "         [24, 25, 26, 27, 28, 29],\n",
      "         [30, 31, 32, 33, 34, 35]],\n",
      "\n",
      "        [[ 0,  1,  2,  3,  4,  5],\n",
      "         [ 6,  7,  8,  9, 10, 11],\n",
      "         [12, 13, 14, 15, 16, 17],\n",
      "         [18, 19, 20, 21, 22, 23],\n",
      "         [24, 25, 26, 27, 28, 29],\n",
      "         [30, 31, 32, 33, 34, 35]],\n",
      "\n",
      "        [[ 0,  1,  2,  3,  4,  5],\n",
      "         [ 6,  7,  8,  9, 10, 11],\n",
      "         [12, 13, 14, 15, 16, 17],\n",
      "         [18, 19, 20, 21, 22, 23],\n",
      "         [24, 25, 26, 27, 28, 29],\n",
      "         [30, 31, 32, 33, 34, 35]],\n",
      "\n",
      "        [[ 0,  1,  2,  3,  4,  5],\n",
      "         [ 6,  7,  8,  9, 10, 11],\n",
      "         [12, 13, 14, 15, 16, 17],\n",
      "         [18, 19, 20, 21, 22, 23],\n",
      "         [24, 25, 26, 27, 28, 29],\n",
      "         [30, 31, 32, 33, 34, 35]],\n",
      "\n",
      "        [[ 0,  1,  2,  3,  4,  5],\n",
      "         [ 6,  7,  8,  9, 10, 11],\n",
      "         [12, 13, 14, 15, 16, 17],\n",
      "         [18, 19, 20, 21, 22, 23],\n",
      "         [24, 25, 26, 27, 28, 29],\n",
      "         [30, 31, 32, 33, 34, 35]]])\n",
      "tensor([[[ 0,  1,  2,  3,  4,  5],\n",
      "         [ 0,  1,  2,  3,  4,  5],\n",
      "         [ 0,  1,  2,  3,  4,  5],\n",
      "         [ 0,  1,  2,  3,  4,  5],\n",
      "         [ 0,  1,  2,  3,  4,  5]],\n",
      "\n",
      "        [[ 6,  7,  8,  9, 10, 11],\n",
      "         [ 6,  7,  8,  9, 10, 11],\n",
      "         [ 6,  7,  8,  9, 10, 11],\n",
      "         [ 6,  7,  8,  9, 10, 11],\n",
      "         [ 6,  7,  8,  9, 10, 11]],\n",
      "\n",
      "        [[12, 13, 14, 15, 16, 17],\n",
      "         [12, 13, 14, 15, 16, 17],\n",
      "         [12, 13, 14, 15, 16, 17],\n",
      "         [12, 13, 14, 15, 16, 17],\n",
      "         [12, 13, 14, 15, 16, 17]],\n",
      "\n",
      "        [[18, 19, 20, 21, 22, 23],\n",
      "         [18, 19, 20, 21, 22, 23],\n",
      "         [18, 19, 20, 21, 22, 23],\n",
      "         [18, 19, 20, 21, 22, 23],\n",
      "         [18, 19, 20, 21, 22, 23]],\n",
      "\n",
      "        [[24, 25, 26, 27, 28, 29],\n",
      "         [24, 25, 26, 27, 28, 29],\n",
      "         [24, 25, 26, 27, 28, 29],\n",
      "         [24, 25, 26, 27, 28, 29],\n",
      "         [24, 25, 26, 27, 28, 29]],\n",
      "\n",
      "        [[30, 31, 32, 33, 34, 35],\n",
      "         [30, 31, 32, 33, 34, 35],\n",
      "         [30, 31, 32, 33, 34, 35],\n",
      "         [30, 31, 32, 33, 34, 35],\n",
      "         [30, 31, 32, 33, 34, 35]]])\n"
     ]
    }
   ],
   "source": [
    "features = torch.arange(6 * 6).reshape((6, 6))\n",
    "print(features)\n",
    "t = features.expand(5, 6, 6)\n",
    "print(t)\n",
    "print(t.permute(1, 0, 2))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([0, 0, 0, 0, 0, 0])"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.arange(30).reshape((6, 5))\n",
    "torch.argsort(a, dim=1)[:, 0]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10, 13, 13, 1)\n"
     ]
    },
    {
     "data": {
      "text/plain": "(10, 13, 13)"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.random.randn(10, 13, 13, 1)\n",
    "print(arr.shape)\n",
    "np.mean(arr, axis=-1).shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "pytorch",
   "language": "python",
   "display_name": "pytorch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}